package com.example.yvque0312.controller;

import com.alibaba.excel.EasyExcel;
import com.example.yvque0312.dao.ArticleDao;
import com.example.yvque0312.pojo.Article;
import com.example.yvque0312.pojo.Category;
import com.example.yvque0312.service.ArticleService;
import com.example.yvque0312.util.Code;
import com.example.yvque0312.util.PageUtil;
import com.example.yvque0312.util.Result;
import com.example.yvque0312.util.UserListener;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping
public class ArticleController {
    @Autowired
    private ArticleService articleDao;

    @PostMapping("/guanli")
    public Result cha(@RequestBody PageUtil pageUtil) {

        PageInfo<Article> cha = articleDao.cha(pageUtil);

        if (cha != null) {
            System.out.println(cha);
            return new Result(Code.GET_OK, cha, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }

    //假删除
    @RequestMapping("/guanlishan/{id}")
    public Result shanchu(@PathVariable("id") Integer id) {

        int shanchu = articleDao.shanchu(id);
        if (shanchu > 0) {
            return new Result(Code.DELETE_OK, "删除成功");
        } else {
            return new Result(Code.DELETE_ERR, "删除失败");
        }
    }

    //添加文章
    @RequestMapping("/guanliadd")
    public Result tianjia(@RequestBody Article article) {

        int tianjia = articleDao.tianjia(article);
        if (tianjia > 0) {
            return new Result(Code.GET_OK, "添加成功");
        } else {
            return new Result(Code.GET_ERR, "添加失败");
        }
    }


    //修改文章
    @RequestMapping("/guanlixiugai")
    public Result xiugai(@RequestBody Article article) {

        int xiugai = articleDao.xiugai(article);
        if (xiugai > 0) {
            return new Result(Code.GET_OK, "修改成功");
        } else {
            return new Result(Code.GET_ERR, "修改失败");
        }
    }

    @Autowired
    private UserListener ul;

    @PostMapping(value = "/importData")
    public Result importData(MultipartFile file) {

        try {
            // 获取前端上传文件的字节流  读流
            InputStream inputStream = file.getInputStream();
            // 从这个文件流中读取数据并且把这一行数据封装成文章对象  并且把这7个文章对象存放到一个集合中
            /**
             * 第一个参数 客户端的读流
             * 第二个参数 我们需要把excel中每一行封装成什么对象  你就写Article类
             *
             * sheet(0)  读取第一个工作表的数据
             * .doReadSync()  异步读取
             */
            List<Article> list = EasyExcel.read(inputStream)
                    .registerReadListener(ul).
                    head(Article.class)
                    .sheet(0).doReadSync();
            if (list.size() > 0) {
                System.out.println(list);
            }

            return new Result(Code.IMPORT_OK, "导入成功");

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(Code.IMPORT_ERR, "导入失败");
        }
    }


    @RequestMapping("/daochuaadd/{id}")
    public ResponseEntity<byte[]> export( @PathVariable Integer id) {
        try {
            // 查询数据
            List<Article> arts =articleDao.chaquanbiao(id);

            // 使用 try-with-resources 管理 ByteArrayOutputStream 资源
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            // 使用 EasyExcel 写入数据到输出流
            EasyExcel.write(outputStream, Article.class).sheet("文章分类信息").doWrite(arts);
            // 返回响应实体
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=people.xlsx");
            return ResponseEntity
                    .ok()
                    .headers(headers)
                    .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                    .body(outputStream.toByteArray());

        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
